`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    22:18:28 05/01/2014 
// Design Name: 
// Module Name:    bot_control 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module bot_control(	enable, respawn,VS,clk,slow_clk,rst,
							Xsprite,Ysprite   );
	//////////////////////////////////////////////////////////////////////////////////
	input VS,clk,rst, enable,slow_clk;
	input	[7:0]	respawn;
	//////////////////////////////////////////////////////////////////////////////////
	output reg [9:0] Xsprite,Ysprite;
	
	parameter XINIT = 40;
	
	reg 			CatIsMovingDown, CatIsMovingRight;
	reg [8:0]	SpriteTimer;
	
	initial begin
		SpriteTimer <= {respawn[6:0],2'b0} + 9'd275;
		
		Xsprite <= XINIT;
		Ysprite <= 10'd478;
	end	
	
 	//------- CAT AI VERTICAL MOVEMENT ----------	
	always@(posedge slow_clk) begin
		if (rst) begin
			SpriteTimer <= {respawn[6:0],2'b0} + 9'd275;
		end else if(CatIsMovingDown) begin
			SpriteTimer <= {respawn[6:0],2'b0} + 9'd275;
		end
		else begin
			if (!VS && SpriteTimer > 0 )	SpriteTimer <= SpriteTimer - 1'b1;
		end
	end
	
	always@(posedge clk) begin
		if (rst) begin
			CatIsMovingDown <= 1'b0;
		end else	if ( Ysprite >= 450 ) 
			CatIsMovingDown <= 1'b0;
		else if (SpriteTimer == 0) begin
			CatIsMovingDown <= 1'b1;
		end 
	end
	
	always@(posedge clk) begin
		if (rst) begin
			Ysprite <= 10'd450;
		end else if (!enable) begin
			Ysprite <= 10'd450;
		end else if (CatIsMovingDown) begin
			if (!VS && slow_clk)	Ysprite <= Ysprite + 1'b1;
		end else if (enable && SpriteTimer == 0) begin
			if (!VS && slow_clk) Ysprite <= 0;
		end 
	end
	
	
	//------- CAT AI HORIZONTAL MOVEMENT ----------	
	always@(posedge clk) begin
		if (rst) begin
			CatIsMovingRight <= 1'b0;
		end else if (CatIsMovingDown) begin
			if (Xsprite <= XINIT - 15) begin	// if cat is too far left
				CatIsMovingRight <= 1'b1;		// start moving right
			end
			else if (Xsprite >= XINIT + 15) 
				CatIsMovingRight <= 1'b0;
		end else CatIsMovingRight <= 1'b0;
	end
	
	always@(posedge clk) begin
		if (rst) begin
			Xsprite <= XINIT;
		end else if (CatIsMovingDown) begin
			if (CatIsMovingRight) begin
				if (!VS && slow_clk)	Xsprite <= Xsprite + 1'b1;
			end
			else begin
				if (!VS && slow_clk) Xsprite <= Xsprite - 1'b1;
			end 
		end else begin
			Xsprite <= XINIT;
		end
	end

endmodule
